package com.iukcy.hight.controller;

import com.iukcy.hight.model.TClass;
import com.iukcy.hight.service.ClassService;
import com.iukcy.hight.service.RabbitService;
import com.iukcy.hight.service.RedisService;
import com.iukcy.hight.util.CodeMsg;
import com.iukcy.hight.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


@RestController
@RequestMapping("/class")
public class TestController implements InitializingBean {
    private final static Logger logger = LoggerFactory.getLogger(TestController.class);
    private Lock lock = new ReentrantLock();
    @Autowired
    ClassService classService;
    @Autowired
    RedisService redisService;
    @Autowired
    RabbitService rabbitService;

    /**
     * <strong>方法描述 ：</strong> 选课-并发<br/>
     * <strong>创建时间：</strong> 2020/9/19 12:12 下午<br/>
     *
     * @param []
     * @return com.iukcy.hight.util.Result<com.iukcy.hight.util.CodeMsg>
     */
    @RequestMapping("/chose")
    public Result<String> test(@RequestParam String classId, @RequestParam String userId) {
        lock.lock();
        // try catch TODO
        Integer num = Integer.valueOf(redisService.redisGet(classId));
        if (num > 0) {
            num = num - 1;
            redisService.redisSet(classId, String.valueOf(num));
            //选课人数预减掉
            logger.info("class=[{}],num=[{}]", classId, num);
        } else {
            lock.unlock();
            return Result.success("课程已完成" + num);
        }
        lock.unlock();
        return Result.success("选课成功" + num);
    }

    /**
     * <strong>方法描述 ：</strong> 状态轮询<br/>
     * <strong>创建时间：</strong> 2020/9/19 12:12 下午<br/>
     *
     * @param []
     * @return com.iukcy.hight.util.Result<com.iukcy.hight.util.CodeMsg>
     */
    @RequestMapping("/getStatus")
    public Result<CodeMsg> getStatus() {
        return Result.success(CodeMsg.SERVER_ERROR);
    }

    /**
     * <strong>方法描述 ：</strong> 预加载课程数据<br/>
     * <strong>创建时间：</strong> 2020/9/19 12:15 下午<br/>
     *
     * @param []
     * @return void
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        List<TClass> classes = classService.getAll();
        classes.forEach(class1 -> {
            redisService.redisSet(String.valueOf(class1.getId()), String.valueOf(class1.getNum()));
        });
        logger.info("预加载数据............课程数据及报名人数[{}]", classes.size());
    }


    //    @Resource(name = "asyncServiceExecutor")
    //    private Executor asyncServiceExecutor;
    //    @Resource
    //    RestTemplate restTemplate;
    //    @RequestMapping("/hight")
    //    public void test() {
    //        String url = "http://localhost:8080/class/chose?classId=1&userId=%s";
    //        for (int i = 0; i < 1000; i++) {
    //            System.out.println("sq---------" + i);
    //            asyncServiceExecutor.execute(new HightBTestU(restTemplate, String.format(url, i)));
    //        }
    //    }
}
